$('.right-titleicon').on("click",function(e){
	projectVM.showMenulist()
})
let that;
projectVM = new Vue({
	el: "#content",
	data: {
		showSelect:false,
		menuList:[
			{name:'投入',path:'goIn.html'},{name:'退出',path:'goOut.html'},{name:'列表',path:'index.html'},
		],
		typeList:[{name:'变压器1'},{name:'变压器2'},{name:'变压器3'},{name:'变压器4'},],
		showSubmit:false,
		onStatus:'',
		selectText:'变压器1',
		result:'',
		showMenu:false,
		showMorebtn:false,
		advance:'',
		showAdd:false, 
		show_popup:false,
		checkData:"",
		onlyselect:false,
		mDisable:false,
		list:[{
				isLumpFirst:false,
				isLumpFirstAdd:true,
				deviceCode:"",//设备编号
				deviceName:"",//设备名称
				inTime:"",//投入时间
				commandContent:"",//调令内容
				areaCenterCommand:"",//区调/中调令
				monthOutCount:"",//本月此装置退出次数(累计值)
			}],
			exitDataJson:"",//编辑用的数据
	},
	created: function() {
		that = this;
		that.list[0].inTime = that.dateFormate()
		setTimeout(that.getDetail,500);
	},
	components:{
		selectitem
	},
	mounted() {
		// this.$nextTick(this.$refs.selectitem[0].formInline.member = "1")
	},
	methods: {
		//获取详情并展示
		getDetail(){
			let config = mui.neioConfig.getNeioConfig();
			let url = config.URL.queryNetDeviceInfoById;
			//详情数据的Key
			let key = config.KEYS.DEVOICE;
			let mData = mui.neioConfig.getStorage(key);
			if("" == mData || undefined == mData){
				return;
			}
			let data = JSON.parse(mData);
			let param = {};
			param.inOperateId = data.inOperateId;//投入设备ID
			param.deviceCode = data.deviceCode;//设备编码
			//清空 缓存
			mui.neioConfig.setStorage(key,"");
			mui.neiosRequst.httpRequst(url,param,function(ret){
				if(ret.code == 0){
					mui.toast(ret.msg)
					//数据暂存
					let　dataParam = {};
					dataParam.netDeviceOperate = ret.operate;
					dataParam.netDeviceInfo = ret.data;
					that.exitDataJson = dataParam;
					
					let array = [];
					ret.data.forEach((item,i)=>{
						let detailData;
						if(array.length == 0){
							detailData = {
								isLumpFirst:false,
								isLumpFirstAdd:true,
							};	
						}else{
							detailData = {
								isLumpFirst:true,
								isLumpFirstAdd:true,
							};	
						}
			
						detailData.deviceCode = item.deviceCode;
						detailData.deviceName = item.deviceName
						detailData.inTime = item.inTime;
						detailData.commandContent = item.commandContent;
						detailData.areaCenterCommand = item.areaCenterCommand;
						detailData.monthOutCount = item.monthOutCount;
						detailData.deviceInfoId = item.deviceInfoId;
						let position = array.length+1;
						array.push(detailData);
					})
					that.list = array;
					setTimeout(that.setCode, 500);
					//数据展示
					// that.list[0].deviceCode = ret.data[0].deviceCode;
					// that.list[0].deviceName = ret.data[0].deviceName;
					// that.list[0].inTime = ret.data[0].inTime;
					// that.list[0].commandContent = ret.data[0].commandContent;
					// that.list[0].areaCenterCommand = ret.data[0].areaCenterCommand;
					// that.list[0].monthOutCount = ret.data[0].monthOutCount;
				}
				if( ret.code == 1){
					mui.toast(ret.msg)
				}
			},function(err){
				console.log("失败回掉："+JSON.stringify(err))
			})
		},
		setCode(){
			that.list.forEach((its,i)=>{
				let a = that.$refs;
				let GV = 'selectitem'+i
				let chooseData = that.$refs[GV][0].chooseItem;
				that.$nextTick(that.$refs[GV][0].formInline.member = its.deviceCode)
			})
		},
		itemChange(position){
			let GV = 'selectitem'+position
			let chooseData = this.$refs[GV][0].chooseItem;
			that.list[position].deviceName = chooseData.deviceName;
			that.list[position].deviceCode = chooseData.deviceCode;
			that.list[position].monthOutCount = chooseData.monthOutCount
		},
		
		//获取 UUID
		uuidv4() {
			let uuid = mui.neiosCommon.getUuidv4();
			return uuid;
		},
		//验证设备编号
		getDevideCode(str){
			let persionData = JSON.parse(localStorage.getItem("neiosUser"));
			let param = {};
			param.deviceCode = str;
			param.operateType = "1";//投入1退出0
			param.inOperateId = "";//投入的传,有就传没有就空
			param.outOperateId = "" ;//退出的传,有就传没有就空
			param.operateStaffCod = persionData.userId;//当前登陆人
			param.stationCode = localStorage.getItem('neiosStationCode');//当前登陆人场站
			
			let config = mui.neioConfig.getNeioConfig();
			let url = config.URL.checkDeviceValidate;
			mui.neiosRequst.httpRequst(url,param,function(ret){
				if(ret.code == 0){
					mui.toast(ret.msg)
				}
				if( ret.code == 1){
					mui.toast(ret.msg)
				}
			},function(err){
				console.log("失败回掉："+JSON.stringify(err))
			})
		},
		//编辑上传数据
		exitData(){
			//主数据
			let param = {};
			param.netDeviceOperate = {};
			param.netDeviceInfo = [];
			let persionData = JSON.parse(localStorage.getItem("neiosUser"));
			
			param.netDeviceOperate.networkDeviceOperateId = that.uuidv4();//主键
			param.netDeviceOperate.workId = "";// that.uuidv4();//保存没有
			param.netDeviceOperate.workDesc = "";//保存没有 - 对涉网设备的投入信息进行提交
			param.netDeviceOperate.areaCompanyCode = persionData.companyCode;//分公司code
			param.netDeviceOperate.newAreaCode = persionData.companyNewCode;//新能源分公司code
			param.netDeviceOperate.stationCode =  persionData.stationCode;//localStorage.getItem('neiosStationCode');//场站code
			param.netDeviceOperate.newStationCode = localStorage.getItem('neiosStationCode') == null ? '1020' : localStorage.getItem('neiosStationCode');//新能源场站code
			param.netDeviceOperate.operateStaffCode = persionData.userId;//当前登陆人ID
			param.netDeviceOperate.operateStaffName = persionData.name;//当前登陆人Name
			param.netDeviceOperate.operateType = "1";//1代表投入 固定值
			param.netDeviceOperate.operateTime = "";// <<例子里没传>>
			param.netDeviceOperate.remark = "";// <<例子里没传>>
			param.netDeviceOperate.client = "NEIOS";//固定值
			param.netDeviceOperate.dataSource = "NEIOS_APP"+localStorage.getItem("currVersion");
			param.netDeviceOperate.status = "32";//提交 32 保存 21
			param.netDeviceOperate.dataStatus = "1";//固定值 1 有效 0 无效
			param.netDeviceOperate.createBy = persionData.userId;//创建人Id
			param.netDeviceOperate.createStaffName = persionData.name;//创建人姓名
			param.netDeviceOperate.createDate = "";//创建时间
			param.netDeviceOperate.modifyBy = persionData.userId;//当前登陆人工号
			param.netDeviceOperate.modifyStaffName = persionData.name;//当前登陆人姓名
			param.netDeviceOperate.modifyDate = "";//修改时间
			
			let userInfor = {};
			userInfor.userId = persionData.userId;
			userInfor.name = persionData.name;
			param.currentUser = userInfor;
			
			let data = that.childDataCreate(param);
			param.netDeviceInfo = data;
			return param;
			
		},
		//编辑子表数据
		childDataCreate(param){
			let mList = that.list;
			mList.forEach((childData,i)=>{
				childData.addif = false;
				childData.delif = false;
				childData.inTime = that.dateFormate();//存入时间
				childData.showTree = false;
				let test = param.netDeviceInfo[i];
				childData.deviceInfoId = param.netDeviceInfo[i] == undefined ? that.uuidv4() : param.netDeviceInfo[i].deviceInfoId;
				childData.inOperateId = param.netDeviceOperate.networkDeviceOperateId;
				childData.areaCompanyCode = param.netDeviceOperate.areaCompanyCode;
				childData.stationCode = param.netDeviceOperate.stationCode;
				childData.newAreaCode = param.netDeviceOperate.newAreaCode;
				childData.newStationCode = localStorage.getItem('neiosStationCode');
				childData.status = "32";
				childData.dataStatus = "1";
				childData.dataSource = param.netDeviceOperate.dataSource;
				childData.client = "NEIOS";
				childData.createBy = param.netDeviceOperate.createBy;
				childData.createStaffName = param.netDeviceOperate.createStaffName;
				childData.modifyBy = param.netDeviceOperate.modifyBy;
				childData.modifyStaffName = param.netDeviceOperate.modifyStaffName;
			})
			
			return mList;
			// that.doPhsu(param);
		},
		//保存数据
		saveData(params){
			
			let param = params;
			
			if(that.exitDataJson != ""){
				let child  = that.childDataCreate(that.exitDataJson)
				that.exitDataJson.netDeviceInfo = child;
				param = that.exitDataJson;
			}
			param.netDeviceOperate.workId = "";
			param.netDeviceOperate.workDesc = "";
			param.netDeviceOperate.status = "21";
			param.netDeviceInfo.forEach((item,position) =>{
				item.status = "21"
			})
			return param;
		},
		//提交数据
		commitData(params){
			let param = params;
			if(that.exitDataJson != ""){
				let child  = that.childDataCreate(that.exitDataJson)
				that.exitDataJson.netDeviceInfo = child;
				param = that.exitDataJson;
			}
			param.netDeviceOperate.workId = that.uuidv4();
			param.netDeviceOperate.workDesc = "对涉网设备的投入信息进行提交";
			param.netDeviceOperate.status = "32";
			param.netDeviceInfo.forEach((item,position) =>{
				item.status = "32"
			})
			param.workType = "0204";
			return param;
		},
		//进行提交操作
		/**
		 * @param {Object} type 类型 32 提交 21 保存
		 */
		doPhsu(type){
			
			let message = type == "21" ? "是否保存" : "是否提交";
			mui.confirm(message,'提示',['取消','确认'],function (e) {
				if(1 == e.index){
					that.list.forEach((item,i)=>{
						// if(that.list[i].deviceCode == ""){
							let GV = 'selectitem'+i
							item.deviceCode = that.$refs[GV][0].formInline.member;
						// }
					})
					let param = {};
					if('32' == type){
						param = that.commitData(that.exitDataJson != "" ? that.exitDataJson : that.exitData());
					}
					if("21" == type){
						param = that.saveData(that.exitDataJson != "" ? that.exitDataJson : that.exitData());
					}
					var checkData = param.netDeviceInfo;
					mui.neioConfig.setNetData(param.netDeviceInfo,"in",function(fun,newData){
						param.netDeviceInfo = newData;
						let isNuLL = mui.neioConfig.checkNetWorkList(param.netDeviceInfo,"in")
						if(!isNuLL) return
						let config = mui.neioConfig.getNeioConfig();
						let url = config.URL.saveOrUpdateNetDevice;
						
						mui.neiosRequst.httpRequst(url,param,function(ret){
							if(ret.code == 0){
								mui.toast(ret.msg)
								that.goBack();
							}
							if( ret.code == 1){
								mui.toast(ret.msg)
							}
						},function(err){
							console.log("失败回掉："+JSON.stringify(err))
						})
					});
				}
			},'div')
			
		},
		// 获取时间
		dateFormate () {
			return mui.neiosCommon.transTime(new Date(),"min")
		},
		//获取毫秒
		getMiu(){
			let date = that.dateFormate();
			var arr = date.split(/[- : \/]/);
			var startDate = Date.parse(new Date(arr[0], arr[1]-1, arr[2], arr[3], arr[4]));
			return startDate;
		},
		//选择变压器类型
		chooseType(e){
		},
		//返回上一页
		goBack(){
			let pageId = plus.webview.getWebviewById("dailyRecord/network/index.html");
			mui.fire(pageId, 'ttTT', {result:"result"});
			mui.back()
		},
		//更多按钮
		showMore(){
			this.showMorebtn ? this.showMorebtn = false : this.showMorebtn = true
		},
		//阻止事件冒泡
		clickStop(){

		},
		//选择是否为值班人
		checkOnstatus(){
			this.onStatus ? this.onStatus = false : this.onStatus =true
			
		},
		//点击菜单
		menuClick(e){
			if(e.name == '我值班'){
				this.onStatus? this.onStatus= false : this.onStatus=true
			}else{
				this.navTo(e.path)
			}
		},
		//左上角菜单显示隐藏
		showMenulist(){
			this.showMenu?this.showMenu=false:this.showMenu=true
		},
		//添加新的检查单
		addLupm(){
			this.list.forEach( (item,i) =>{
				if(i == 0){
					item.isLumpFirst = true;
					item.isLumpFirstAdd = false;
				}
			})
			let newLumpItem = {
				isLumpFirst:true,
				isLumpFirstAdd:true,
				deviceCode:"",//设备编号
				deviceName:"",//设备名称
				inTime:that.dateFormate(),//投入时间
				commandContent:"",//调令内容
				areaCenterCommand:"",//区调/中调令
				monthOutCount:"",//本月此装置退出次数(累计值)
			}
			this.list.push(newLumpItem)
		},
		//移除检查单
		removeLupm(position){
			
			//移除页面传过来的对应的 item 
			if(that.exitDataJson != ""){
				that.exitDataJson.netDeviceInfo.forEach((item,i)=>{
					if(item.deviceInfoId == this.list[position].deviceInfoId){
						that.exitDataJson.netDeviceInfo.splice(i,1);
					}
				})
			}
			this.list.splice(position,1)
			if(this.list.length <= 1){
				this.list[0].isLumpFirst = false;
				this.list[0].isLumpFirstAdd = true;
			}else{
				let lumpItemPosition = this.list.length;
				this.list[lumpItemPosition - 1].isLumpFirstAdd = true;
			}
		},
		navTo(e){
			mui.openWindow({
					url: e,
					id: e,
			});
		},
		showTime(position){
		  var dtpicker = new mui.DtPicker({ 
			  "type": "datetime"
		  })
		  dtpicker.show(function(e) { 
			  let inTime = e.value;
			  that.list[position].inTime = inTime;
		  })
		},
	},
	
	})